// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_gray");
    var nature = $("#nature").val();
	var enterpriseId;
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
    
    var init = function(page){
    	enterpriseId = page.params.enterpriseId || base.getUrlParam("enterpriseId");
    	router.init();
    	inquireEnterpriseDetail(enterpriseId);
		initEvent(); 
    };
    
	var initEvent = function(){
		//调显示与隐藏函数
		hideSpan();
		hideCardType();
		$("#main").on("change", "#sUserType", function(){
			hideSpan();
		});
		$("#main").on("change", "#scardType", function(){
			hideCardType();
		});
		
		$("#main").on("change", "#creditCard", function(){
			var bn = tools.getBankName($(this).val());
			if (bn == ""){
				alert("未知开户行！");
			} else {
				$("#depositBank").val(bn);
			}
		});
		
		$("#main").on("change", "#legalIdcard", function(){
			var promptString = validate.isIdCard($(this), "请输入正确的身份证号码!");
			if ("" != promptString) {
				base.alert(2, promptString)
			} 
		});
		
		$("#main").on("focus", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		$("#main").on("change", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		//认证提交
		$("#main").on("click", ".next", function(){
			databind.submitDom($("#clientAuthentication_details"), function(){
				base.alert(1, "提交成功！");
				location.href = "../client/clientManage.html";
		  	});
		});

    };
    
  //查询企业详细信息
	var inquireEnterpriseDetail = function(enterpriseId) {
		new PCAS("province","city","district");
		base.fnAjax({
			action: "inquireEnterpriseDetail.json",
			oData: {
				enterpriseId: enterpriseId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					//判断认证状态
					databind.disableInput($("#clientAuthentication_details"));
					if (data.datas[0].enterpriseGradeEvaluationDetail != null) {
						databind.fillForm($("#main"), data.datas[0]);
					}
					$("#city").html('<option value="'+data.datas[0].city+'">'+data.datas[0].city+'</option>');
					$("#district").html('<option value="'+data.datas[0].district+'">'+data.datas[0].district+'</option>');
					
					
				} else {
					base.alert(2, data.message);
				}
			}
		});
		
	};
    
	//联动1
	var hideSpan = function() {
		var sUserType = $("#sUserType").val();
		if(sUserType == "以代理人身份注册") {
			$("#hide1").show();
			$("#hide2").show();
			$("#procurator").attr('isNull',1);
			$("#procuratorIdcard").attr('isNull',1);
			$("#procuratorPeriod").attr('isNull',1);
		}
		if(sUserType == "以法定代表人身份注册") {
			$("#hide2").hide();
			$("#hide1").show();
			$("#procurator").attr('isNull',0);
			$("#procuratorIdcard").attr('isNull',0);
			$("#procuratorPeriod").attr('isNull',0);
		}
	};
	
	//联动2
	var hideCardType = function() {
		var scardType = $("#scardType").val();
		if(scardType == "普通营业执照（存在独立的组织机构代码证）") {
			$("#hideRegNo").show();
			$("#certificateShow").show();
			$("#codeUSCHide").hide();
			$("#codeUSC").attr('isNull',0);
			$("#regNo").attr('isNull',1);
			$("#certificate").attr('isNull',1);
		}
		if(scardType == "多证合一营业执照（不存在独立的组织机构代码证）") {
			$("#hideRegNo").hide();
			$("#certificateShow").hide();
			$("#codeUSCHide").show();
			$("#codeUSC").attr('isNull',1);
			$("#regNo").attr('isNull',0);
			$("#certificate").attr('isNull',0);
		}
	};
	
    return {
        init:init
    };
});